package com.hison.tandainbao.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.hison.tandainbao.entiy.pojo.domain.TbLocalevent;
import com.hison.tandainbao.entiy.pojo.domain.User;
import com.hison.tandainbao.entiy.pojo.dto.TbLocaleventDto;
import com.hison.tandainbao.entiy.pojo.dto.UserDTO;
import com.hison.tandainbao.mapper.TbLocaleventMapper;
import com.hison.tandainbao.mapper.UserMapper;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbLocaleventService;
import com.hison.tandainbao.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hison
 * @description 针对表【tb_localevent】的数据库操作Service实现
 * @createDate 2024-03-29 22:54:55
 */
@Service
public class TbLocaleventServiceImpl extends ServiceImpl<TbLocaleventMapper, TbLocalevent>
        implements TbLocaleventService {

    @Autowired
    private TbLocaleventMapper mapper;


    /**
     * 获取所有事件
     *
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult getAllEvents() {
        List<TbLocalevent> list = query().eq("status", 2).orderByAsc("create_time").list();
        if (list == null) {
            return ResponseResult.error("查询为空");
        }
        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult add(TbLocaleventDto tbLocaleventDto) {
        //获取当前登录用户的id
//        Long id = UserHolder.getUser().getId();
        //将信息进行重新封装
        tbLocaleventDto.setUserid(1L);
        //设置文化信息发布状态为待审核状态
        tbLocaleventDto.setStatus(1);
        //保存上传的图片信息（由前端封装）
        //将文化信息表 ，同时保存到状态审核表
        //1.先将信息存入信息表
        int insert = mapper.insert(tbLocaleventDto);
        if (insert < 1) {
            return ResponseResult.error("添加失败");
        }
        return ResponseResult.ok("添加成功，等待管理员审核");
    }

    @Override
    public ResponseResult saveDraftEvent(TbLocalevent tbLocalevent) {
        tbLocalevent.setUserid(UserHolder.getUser().getId());
        tbLocalevent.setStatus(0);
        tbLocalevent.setCreateTime(DateTime.now());
        tbLocalevent.setUpdateTime(DateTime.now());
        int i = mapper.insert(tbLocalevent);
        if (i < 1) {
            return ResponseResult.error("保存失败");
        }
        return ResponseResult.ok("保存成功");
    }

    /**
     * 更新活动信息
     *
     * @param tbLocaleventDto 结核病本地事件 DTO
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see TbLocaleventDto
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult updateEventInfo(TbLocaleventDto tbLocaleventDto) {
        boolean b = updateById(tbLocaleventDto);
        if (!b) {
            return ResponseResult.error("更新失败");

        }
        return ResponseResult.ok("更新成功");
    }

    /**
     * 按用户名获取事件信息
     *
     * @param userName 用户名
     * @return @return {@code ResponseResult }
     * @author $USER$
     * @date 2024/03/29
     * @see String
     * @see ResponseResult
     * @since version
     */
    @Override
    public ResponseResult getEventInfoByUserName(String userName) {
        List<TbLocalevent> list = mapper.getCultureInfoByUserName(userName);
        if (list.size() == 0) {
            return ResponseResult.error("该用户还未发布信息");
        }
        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult putCulture(Integer eventId) {
        Long id = UserHolder.getUser().getId();
        int i = mapper.updateInfo(eventId, id);
        if (i < 1) {
            return ResponseResult.error("发布失败");
        }
        return ResponseResult.ok("发布成功");
    }

    @Override
    public ResponseResult delete(Integer eventId) {
        Long id = UserHolder.getUser().getId();
        QueryWrapper<TbLocalevent> wrapper = new QueryWrapper<>();
        wrapper.eq("event_id", eventId);
        wrapper.eq("userId", id);
        int delete = mapper.delete(wrapper);
        if (delete < 1) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

    @Override
    public ResponseResult getMyEvent() {
        QueryWrapper<TbLocalevent> wrapper = new QueryWrapper<>();
//        wrapper.eq("userId", UserHolder.getUser().getId());
        wrapper.eq("userId", 1L);
        List<TbLocalevent> list = mapper.selectList(wrapper);
        if (list.size() == 0) {
            return ResponseResult.ok("查询结果为空");
        }
        return ResponseResult.ok("查询成功", list);
    }

    @Override
    public ResponseResult getMySaveDraftEvent() {
        QueryWrapper<TbLocalevent> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", UserHolder.getUser().getId());
        wrapper.eq("status", 0);
        List<TbLocalevent> list = mapper.selectList(wrapper);
        if (list.size() == 0) {
            return ResponseResult.ok("查询结果为空");
        }
        return ResponseResult.ok("查询成功", list);
    }


    // -------------------------------管理员接口 ---------------------------------
    @Override
    public ResponseResult checkLocalEvent(int eventId, String status) {
        int i = mapper.checkLocalCuture(eventId, status);
        if (i < 1) {
            return ResponseResult.error("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    @Override
    public ResponseResult getAllEvent(Integer current,  Integer total) {
        PageHelper.startPage(current,total);
        List<TbLocalevent> cultures = query().orderByAsc("status","create_time").list();
        //根据用户id查询用户名
        cultures.forEach(item ->{
            item.setUserName(getUserName(item.getUserid()));
        });
        return ResponseResult.ok("查询成功", cultures);
    }

    @Autowired
    private UserMapper userMapper;
    private String getUserName(Long userid) {
        User user = userMapper.selectById(userid);
        return user.getName();
    }

    @Override
    public ResponseResult deleteEventsByAdmin(Integer eventId) {
        int i = mapper.deleteById(eventId);
        if (i < 1) {
            return ResponseResult.error("删除失败");
        }
        return ResponseResult.ok("删除成功");
    }

}
